(function(){
    angular.module('app').controller("agbBiddingCtrl",function($scope,proxy){
        $scope.permission = PERMISSION;
        $scope.targetList = [
            {
                title: '广告主花费',
                t:'advertiserCost',
            },{
                title: '代理商成本',
                t:'agencyCost',
            },{
                title: '利润',
                t:'agencyProfit',
            },{
                title: '利润率',
                t:'agencyDealProfitRate'
            },{
                title: '展示量',
                t:'view'
            },{
                title: '点击量',
                t:'click'
            }
        ];
        
        // 注释见agbIndexCtrl.js
        $scope.$watch('permission',function (newValue, oldValue) {
            if(angular.equals({},newValue)) return;
            console.log(1)
            setTatgetList(newValue)
        });
        $scope.$on('permission',function (e, permission) {
            console.log(2)
            setTatgetList(permission)
        });
        $scope.sTime = getNowFormatDate() + " 00:00:00";
        $scope.eTime = getNowFormatDate() + " 23:59:59";
        $scope.chooseDate = function (start, end) {
            if(start) {
                $scope.sTime = start + " 00:00:00";
                $scope.eTime = end + " 23:59:59";
            } else {
                $scope.sTime = $scope.eTime = undefined;
            }
            getList();
            getSum();
        }
        // 下载报表
        $scope.download = function() {
            var data = {
                'page': "1",
                'size': "99999",
                'filter': {},
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/rtb/deal',
                load: true,
                data: data,
                success: function(res){
                    var arr = res.data.data || [];
                    // 获得列表
                    arr.forEach(function (v) {
                        v.agencyDealProfitRate = (v.agencyDealProfitRate * 100).toFixed(2) + "%";
                        v.clickRate = (v.clickRate * 100).toFixed(2) + "%";
                    })
                    exportCsv({
                        title: ['公司','广告主花费','代理商成本','利润','利润率','展示量','点击量','点击率','CPM','CPC'],
                        titleForKey: ['advertiserName','advertiserCost','agencyCost','agencyProfit','agencyDealProfitRate','view','click','clickRate','cpm','cpc'],
                        data: arr,
                    },"账户概况.csv");
                }
            })
        }
        getSum();
        getList();

        function getTotal() {
            var data = {
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/rtb/deal/sum',
                load: true,
                data: data,
                success: function(res){
                    $scope.total = res.data.data;
                }
            })
        }
        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                'filter': {},
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/rtb/deal',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;
                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }

        function getSum() {
            var data = {
                "sort": ["cTime,1"],
                'filter': {},
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/rtb/deal/sum',
                load: true,
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.sum = res.data.data[0] || {
                            "advertiserCost":"0",//花费
                            "agencyProfit":"0",//利润
                            "agencyDealProfitRate":"0",//利润率
                            "dspCost":"0",//成本
                            "view":"0",//展示量
                            "click":"0",//点击量
                            "clickRate":"0",
                            "agencyCost":"0",
                            "cpm": "0",
                            "cpc": "0",
                        };
                    $scope.sum.agencyDealProfitRate = ($scope.sum.agencyDealProfitRate*100).toFixed(2)+"%";
                    $scope.sum.clickRate = ($scope.sum.clickRate*100).toFixed(2)+"%";
                }
            })
        }
         // 根据权限变更指标
        function setTatgetList(permission) {
            var targetList = $scope.targetList;
            if(!permission.view['报表中心'].sub['竞价交易概况'].sub || !permission.view['报表中心'].sub['竞价交易概况'].sub['广告主利润']) {
                // 移除‘代理商成本’ ‘利润’ ‘利润率’
                for(var i = 0; i < targetList.length; i++) {
                    if(targetList[i].t == 'agencyCost') {
                        targetList.splice(i,3);
                        break;
                    }
                }
            }
            // 指标一行为6个  不足6的倍数时  补充空对象
            while(targetList.length % 6 != 0) {
                targetList.push({});
            }
            $scope.permission = permission;
        }
    })
})();